//
//  WSAuthorCell.m
//  GroupFourFM
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WSAuthorCell.h"
#import "WSAuthorItem.h"

@interface WSAuthorCell ()<UICollectionViewDataSource, UICollectionViewDelegate, WSAuthorItemDelegate>

@property(nonatomic,retain)UICollectionView *myCollectionView;
@property(nonatomic,retain)NSMutableArray *myArray;

@end

@implementation WSAuthorCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createCell];
    }
    
    return self;
}

#pragma mark - 创建cell子视图
-(void)createCell {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(7, 15, 10, 15);
    flowLayout.minimumLineSpacing = 15;
    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 60) / 3, SCREEN_WIDTH * 0.5 - 17);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
    [self addSubview:self.myCollectionView];
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    self.myCollectionView.showsHorizontalScrollIndicator = NO;
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    [self.myCollectionView registerClass:[WSAuthorItem class] forCellWithReuseIdentifier:@"authorColloectionCell"];
    [self.myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

#pragma mark - collectionView delegate && dataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.myArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WSAuthorItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"authorColloectionCell" forIndexPath:indexPath];
    
    cell.delegate = self;
    cell.authorModel = [self.myArray objectAtIndex:indexPath.row];
    
    return cell;
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WSAuthorItem *cell = (WSAuthorItem *)[collectionView cellForItemAtIndexPath:indexPath];
    [self.AuthorDelegate JumpToAuthorView:cell.headImageView.tag :_myTitle];
    
}

-(void)setAllAuthorArray:(NSMutableArray *)allAuthorArray {
    
    _allAuthorArray = allAuthorArray;
    self.myArray = [NSMutableArray arrayWithArray:self.allAuthorArray];
    [self.myCollectionView reloadData];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - WSAuthorItem 代理方法
- (void)collectedWithModel:(WSAuthorModel *)model {

    [self.AuthorDelegate collectedWithModeltoController:model];
}

@end
